package com.biostime.dingdingbox.modules.bt.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.dingdingbox.common.config.Global;
import com.biostime.dingdingbox.common.persistence.Page;
import com.biostime.dingdingbox.common.utils.StringUtils;
import com.biostime.dingdingbox.common.web.BaseController;
import com.biostime.dingdingbox.modules.bt.entity.PubNumber;
import com.biostime.dingdingbox.modules.bt.service.PubNumberService;

/**
 * 公众号管理Controller
 * @author cn
 * @version 2016-08-17
 */
@Controller
@RequestMapping(value = "${adminPath}/bt/pubNumber")
public class PubNumberController extends BaseController {

	@Autowired
	private PubNumberService pubNumberService;
	
	@ModelAttribute
	public PubNumber get(@RequestParam(required=false) String id) {
		PubNumber entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = pubNumberService.get(id);
		}
		if (entity == null){
			entity = new PubNumber();
		}
		return entity;
	}
	
	
	@RequestMapping(value = {"list", ""})
	public String list(PubNumber pubNumber, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PubNumber> page = pubNumberService.findPage(new Page<PubNumber>(request, response), pubNumber); 
		model.addAttribute("page", page);
		return "modules/bt/pubNumberList";
	}

	
	@RequestMapping(value = "form")
	public String form(PubNumber pubNumber, Model model) {
		model.addAttribute("pubNumber", pubNumber);
		return "modules/bt/pubNumberForm";
	}

	
	@RequestMapping(value = "save")
	public String save(PubNumber pubNumber, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, pubNumber)){
			return form(pubNumber, model);
		}
		pubNumberService.save(pubNumber);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/bt/pubNumber/?repage";
	}
	
	
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, PubNumber pubNumber, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				pubNumberService.delete(new PubNumber(id));
			}
		}else{
			pubNumberService.delete(pubNumber);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/bt/pubNumber/?repage";
	}
	


}